package sn.gandal.mobile.kalpet.data.entities;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import sn.gandal.mobile.kalpet.server.dto.HistoriqueDTO;
import sn.gandal.mobile.kalpet.utils.KalpetConstants;

import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;

import android.content.Context;

public class DatabaseManager {

	static private DatabaseManager instance;
    private DatabaseHelper helper;
    private DataBaseCrudHelper crud;

    static public void init(Context ctx) {
        if (null==instance) {
            instance = new DatabaseManager(ctx);
        }
    }

    static public DatabaseManager getInstance() {
        return instance;
    }


    private DatabaseManager(Context ctx) {
        helper = new DatabaseHelper(ctx);
        crud=new DataBaseCrudHelper(helper);
    }

   

    public List<Account> getAllAccountLists() {
        List<Account> account = null;
        try {
            account = getHelper().getAccountListDao().queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return account;
    }
    
    public int save(Object obj){
    	return getCrud().save(obj);
    }
    
    
    public Account getAccountById(int id){
    	Account  account=null;
    	try {
    		account=getHelper().getAccountDao().queryForId(id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return account;
    }

	public boolean isUserValid(String numeroCli, String codeSecret) {
		
		try {
			if(getAccountByCodeClientAndCodeSecret(numeroCli, codeSecret) != null)
				return true;
		} catch (SQLException e) {			
			e.printStackTrace();
		}
    	return false;
	}
	
	public boolean isUserApplicationExiste(){
		List<Credentials> crd;
		try {
			crd = getHelper().getCredentialsDao().queryForAll();
			if(crd !=null && crd.size() >0)
				return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	
	  return false;
	}
	
	public Credentials getAccountByCodeClientAndCodeSecret(String numeroCli, String codeSecret) throws SQLException{
		
		QueryBuilder<Credentials,Integer> queryBuilder =getHelper().getCredentialsDao().queryBuilder();
				
		List<Credentials> credentials =  queryBuilder.where()
													.eq(Credentials.CODE_SECRET, codeSecret)
													.and()
													.eq(Credentials.NUMERO_CLIENT, numeroCli)
													.query();
													
		if(credentials !=null && credentials.size()> 0)
			return credentials.get(0);				
		
		return null;
	}

	public DataBaseCrudHelper getCrud() {
		return crud;
	}

	 private DatabaseHelper getHelper() {
	        return helper;
	    }

	public List<HistoriqueDTO> geHistoriqueByTypeAndDate(Date dateUpdate, KalpetConstants.TYPE_COMPTE type ) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
